package net.dipper.tankwar.game.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.xml.parsers.ParserConfigurationException;

import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.core.helper.GameFileManager;
import net.dipper.tankwar.game.helper.MessageManager;

import org.xml.sax.SAXException;

import wnb.util.config.ConfigurationManager;

public class SelectionPane extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final int DEFAULT_W = 300;
	
	private String dirGames;
	private String dirSaves;
	private MainWindow parent;
	private JButton btnSelect;
	private JList<String> listGames;
	private JButton btnNewGame;
	private JList<String> listSaves;
	private JButton btnContinue;
	private JButton btnDelete;
	
	public SelectionPane(MainWindow parent){
		try {
			this.parent = parent;
			
			dirGames = ConfigurationManager.getAppSetting("dir_games");
			dirSaves = ConfigurationManager.getAppSetting("dir_saves");
			
			btnSelect = new JButton("选择文件夹");
			listGames = new JList<String>();
			btnNewGame = new JButton("新游戏");
			listSaves = new JList<String>();
			btnContinue = new JButton("继续游戏");
			btnDelete = new JButton("删除");
			
			refreshList(dirGames, listGames);
			refreshList(dirSaves, listSaves);
			
			JScrollPane paneGames = new JScrollPane(listGames);
			JScrollPane paneSaves = new JScrollPane(listSaves);
			btnSelect.setPreferredSize(new Dimension(DEFAULT_W, 32));
			btnNewGame.setPreferredSize(new Dimension(DEFAULT_W, 32));
			paneGames.setPreferredSize(new Dimension(DEFAULT_W, 100));
			paneSaves.setPreferredSize(new Dimension(DEFAULT_W, 100));
			btnContinue.setPreferredSize(new Dimension(DEFAULT_W/2, 32));
			btnDelete.setPreferredSize(new Dimension(DEFAULT_W/2, 32));
			
			btnSelect.addActionListener(this);
			btnNewGame.addActionListener(this);
			btnContinue.addActionListener(this);
			btnDelete.addActionListener(this);
			
			GridBagLayout layout = new GridBagLayout();
			setLayout(layout);
			add(btnSelect, layoutComponent(0, 0, 2));
			add(new JLabel("选择地图"),  layoutComponent(0, 1, 2));
			add(paneGames, layoutComponent(0, 2, 2));
			add(btnNewGame, layoutComponent(0, 3, 2));
			add(new JLabel("已保存游戏"),  layoutComponent(0, 4, 2));
			add(paneSaves, layoutComponent(0, 5, 2));
			add(btnContinue, layoutComponent(0, 6, 1));
			add(btnDelete, layoutComponent(1, 6, 1));
		} catch (ParserConfigurationException | SAXException | IOException e) {
			e.printStackTrace();
		}
	}

	private GridBagConstraints layoutComponent(int i, int j, int spanX) {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = i;
		c.gridy = j;
		c.gridwidth = spanX;
		c.weightx = 0.0;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(5, 5, 5, 5);
		
		return c;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			Object source = e.getSource();
			if(source.equals(btnSelect)){
				selectGamesDir();
			}else if(source.equals(btnNewGame)){
				newGame();
			}else if(source.equals(btnContinue)){
				loadGame();
			}else if(source.equals(btnDelete)){
				deleteGame();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	//删除游戏
	private void deleteGame(){
		String fileName = (String) listSaves.getSelectedValue();
		if(fileName == null)
			return;
		
		File file = new File(dirSaves, fileName);
		file.delete();
		refreshList(dirSaves, listSaves);
	}
	
	//加载已保存游戏
	private void loadGame() throws FileNotFoundException, IOException{
		String fileName = (String) listSaves.getSelectedValue();
		if(fileName == null)
			return;
		
		File file = new File(dirSaves, fileName);
		createNewGame(file);
	}

	//创建新游戏
	private void newGame() throws FileNotFoundException, IOException {
		String fileName = (String) listGames.getSelectedValue();
		if(fileName == null)
			return;
		
		File file = new File(dirGames, fileName);
		createNewGame(file);
	}
	
	private void createNewGame(File file) throws FileNotFoundException, IOException{
		MessageManager manager = MessageManager.getInstance();
		manager.clear();
		
		ObjectMap map = GameFileManager.load(new FileInputStream(file));
		GameContext context = new GameContext(map);
		OperationPane pane = new OperationPane(parent, context);
		context.start();
		parent.setPane(pane);
		parent.setGameControl(context.getControl());
		
	}

	//选择游戏文件夹
	private void selectGamesDir() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int result = chooser.showOpenDialog(this.parent);
		if(result == JFileChooser.APPROVE_OPTION){
			File selected = chooser.getSelectedFile();
			this.dirGames = selected.getAbsolutePath();
			refreshList(this.dirGames, this.listGames);
		}
	}

	//刷新游戏文件夹显示
	private void refreshList(String dir, JList<String> list) {		
		FilenameFilter filter = new FilenameFilter(){
			@Override
			public boolean accept(File file, String path) {
				return path.endsWith(".twg");
			}
		};
		
		File file = new File(dir);
		list.setListData(file.list(filter));
		list.validate();
	}
}
